package sjtu.ist.warehouse4ws.action;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import sjtu.ist.warehouse4ws.model.Privilege;
import sjtu.ist.warehouse4ws.service.FileInfoService;
import sjtu.ist.warehouse4ws.service.PrivilegeService;
import sjtu.ist.warehouse4ws.tool.ListPage;
import sjtu.ist.warehouse4ws.vo.FullPrivilegeVO;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PrivilegeAction extends ActionSupport{
	private FileInfoService fileInfoService;
	private PrivilegeService privilegeService;
	private List<FullPrivilegeVO> privilegeList = new ArrayList<FullPrivilegeVO>();
	
	private String id;
	private Integer deleteop;
	private Integer readop;
	private Integer modifyop;
	
	private String fileName;
	private String project;
	private String version;
	private Integer type;
	private String department;
	private String company;
	
	private boolean success=true;
	private String fileInfoId;
	private String op;
	
	private int start;
	private int limit;
	private int count;

	
	//获得Privilege列表
	public String showPrivilegeList(){
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();
		
		String userCompany=session.get("user_company").toString();
		int userType=(Integer)(session.get("user_type"));
		
		//如果是普通用户访问
		if(userType==2){
			success=false;
			return SUCCESS;
		}else if(userType==0){
			ListPage lp= privilegeService.getAllFullPrivileges(start,limit);
			this.privilegeList = lp.list;
			count=lp.total;
		}else if(userType==1){
			ListPage lp=privilegeService.searchFullPrivilege(null, null, null, null, null, null, userCompany,start,limit);
			this.privilegeList = lp.list;
			count=lp.total;
		}
		
		return SUCCESS;
	}
	
	//更新权限
	public String updatePrivilege(){

		Privilege privilege=privilegeService.get(id);
		privilege.setDeleteop(deleteop);
		privilege.setModifyop(modifyop);
		privilege.setReadop(readop);
		privilegeService.update(privilege);
		return SUCCESS;
	}
	
	//搜索权限
	public String searchPrivilege(){
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();
		
		String userCompany=session.get("user_company").toString();
		int userType=(Integer)(session.get("user_type"));
		
		try {
			if(fileName != null){
			fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8"); }
			if(project != null){
			project = new String(project.getBytes("ISO-8859-1"), "UTF-8");}
			//keyWord3 = new String(keyWord3.getBytes("ISO-8859-1"), "UTF-8");
			if(version != null){
			version = new String(version.getBytes("ISO-8859-1"), "UTF-8");} 
			//keyWord6 = new String(keyWord6.getBytes("ISO-8859-1"), "UTF-8"); 
			} catch (UnsupportedEncodingException e) { 
			// TODO 自动生成 catch 块 
			e.printStackTrace(); 
			} 
			
			System.out.println("aaaaaaa"+project);
		
		//如果是普通用户访问
		if(userType==2){
			success=false;
			return SUCCESS;
		}else if(userType==1){
			ListPage lp = privilegeService.searchFullPrivilege(fileName, project, version, type, department, company, userCompany,start,limit);
			this.privilegeList = lp.list;
			count=lp.total;
		}else if(userType==0){
			ListPage lp = privilegeService.searchFullPrivilege(fileName, project, version, type, department, company, null,start,limit);
			this.privilegeList = lp.list;
			count=lp.total;
		}
		
		
		return SUCCESS;
	}
	
	//验证权限
	public String checkPrivilege(){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();

		String user_id = session.get("user_id").toString();
		String user_company = session.get("user_company").toString();
		int user_type = (Integer)(session.get("user_type"));
		
		String file_com=fileInfoService.getCompanyByFileInfoId(fileInfoId);
		
		success=privilegeService.checkPrivilege(fileInfoId, user_company, user_type, op);
		
		return SUCCESS;
	}
	
	@JSON(name = "totalPrivilege")
	public int getTotalPrivilege() {
		//return this.privilegeList.size();
		return count;
	}

	@JSON(name = "Privilege")
	public List<FullPrivilegeVO> getPrivilegeList() {
		return this.privilegeList;
	}
	
	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	// 注入PrivilegeService
	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setDeleteop(Integer deleteop) {
		this.deleteop = deleteop;
	}

	public void setReadop(Integer readop) {
		this.readop = readop;
	}

	public void setModifyop(Integer modifyop) {
		this.modifyop = modifyop;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setProject(String project) {
		this.project = project;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getFileInfoId() {
		return fileInfoId;
	}

	public void setFileInfoId(String fileInfoId) {
		this.fileInfoId = fileInfoId;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
}